package com.extension.company.view.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * recycleView adapter的基类
 *
 * @param <T>  子类adapter 中 list<T> 数据中的数据对象。
 * @param <VH> recycleView.holder
 */
public class BaseRecyclerAdapter<T, VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

	/**
	 * 监听接口
	 */
	protected OnRecyclerViewClickListener mOnClickListener;

	/**
	 * 上下文
	 */
	protected Context mContext;
	/**
	 * recycleview 的item的渲染器
	 */
	protected LayoutInflater mInflater;
	/**
	 * 数据
	 */
	protected List<T> list;

	public BaseRecyclerAdapter(Context context, List<T> list) {
		this.mContext = context;
		this.list = list;
		this.mInflater = LayoutInflater.from(mContext);
	}


	@Override
	public VH onCreateViewHolder(ViewGroup parent, int viewType) {
		return null;
	}

	@Override
	public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, final int position) {
		if (mOnClickListener != null) {
			viewHolder.itemView.setOnClickListener(new View.OnClickListener() { // recycleView的item设置点击事件
				@Override
				public void onClick(View view) {
					mOnClickListener.onItemClick(viewHolder.itemView, position);
				}
			});
			viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
				@Override
				public boolean onLongClick(View view) {              // recycleView的item设置长点击事件
					mOnClickListener.onItemLongClick(viewHolder.itemView, position);
					return false;
				}
			});
		}
	}

	@Override
	public int getItemCount() {
		return list == null ? 10 : list.size();
	}

	/**
	 * recycleView 点击事件
	 *
	 * @param onClickListener
	 */
	public void setOnClickListener(OnRecyclerViewClickListener onClickListener) {
		this.mOnClickListener = onClickListener;
	}

	/**
	 * 提供：网络请求后，填充数据，同时刷新数据
	 *
	 * @param list 数据列表
	 */
	public void setContent(List<T> list) {
		this.list = list;
		this.notifyDataSetChanged();
	}
}
